package com.example.mymessagequeue.mqserver.core;

import com.example.mymessagequeue.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 这个类表示存储消息的队列
 */
public class MsgQueue {
    // 队列的身份标识
    private String name;

    // 队列是否持久化
    private boolean durable = false;

    // 如果为 true 表示这个队列只能被一个消费者使用 n
    private boolean exclusive = false;

    // 自动删除 n
    private boolean autoDelete = false;

    // 扩展参数 n
    private Map<String, Object> arguments = new HashMap<>();

    // 当前队列都有哪些消费者订阅
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();
    // 记录当前取到了第几个消费者, 方便实现轮询策略(AtomicInteger 原子类型)
    private AtomicInteger consumerSeq = new AtomicInteger(0);

    // 添加一个新的订阅者
    public synchronized void addConsumerEnv(ConsumerEnv consumerEnv) {
        System.out.println("[VirtualHost] 消费者上线 consumerTag=" + consumerEnv);
        consumerEnvList.add(consumerEnv);
    }

    // 订阅者的删除
    public synchronized void removeConsumerEnv(String consumerTag) {
        ConsumerEnv consumerEnv = null;
        for (ConsumerEnv item : consumerEnvList) {
            if (item.getConsumerTag().equals(consumerTag)) {
                consumerEnv = item;
                break;
            }
        }
        if (consumerEnv == null) {
            return;
        }
        // 此订阅者不订阅这个队列了
        System.out.println("[VirtualHost] 消费者下线 consumerTag=" + consumerEnv);
        consumerEnvList.remove(consumerEnv);
    }

    // 挑选一个订阅者, 用来处理当前的消息. (按照轮询的方式)
    public ConsumerEnv chooseConsumer() {
        if (consumerEnvList.size() == 0) {
            // 该队列没有人订阅
            return null;
        }
        // 计算当前要取的元素的下标
        int index = consumerSeq.get() % consumerEnvList.size();
        consumerSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public Object getArguments(String key) {
        return arguments.get(key);
    }

    public String getArguments() {
        // 把当前的 arguments 转成 json
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }

    public void setArguments(String key, Object value) {
        this.arguments.put(key, value);
    }

    public void setArguments(String argumentsJson) {
        // 数据库读到的 json 转换成对象
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String, Object>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public void setArguments(Map<String, Object> arguments) {
        this.arguments = arguments;
    }
}
